from util.apiUrl import HOST_URL
from util.csvUtil import CsvUtil
from util.log import logger
from util.yamlUtil import YamlUtil

class RequestDataUtil:
    def __init__(self,fileName):
        self.csvFile = 'D:/File/workspace/案例/interface/config/' + fileName + '.csv'
        self.yamlFile = 'D:/File/workspace/案例/interface/data/' + fileName + '.yml'

    #组建一个列表  是每条用例执行所需要的数据
    def getRequestData(self):
        #获取yaml数据
        yamlUtil =YamlUtil(self.yamlFile)
        requestDataList=[]
        #判断获取的csv数据`
        csvUtil = CsvUtil(self.csvFile)

        for dict in csvUtil.getCsvData():
            yamlDict = yamlUtil.getYamlData()
            #url请求路径
            yamlDict['url'] = HOST_URL# + yamlDict['url']
            #判断请求头
            headers = yamlDict['headers']
            if headers != None:
                for key in headers.keys():
                    yamlDict[key] = dict[key]
            #判断 params 参数

            params = yamlDict['params']
            if params != None:
                for key in params.keys():
                    params[key] = dict[key]

            #判断data参数
            data = yamlDict['data']
            if data != None:
                for key in data.keys():
                    data[key] = dict[key]

            #判断json参数
            json = yamlDict['json']
            if json != None:
                for key in json.keys():
                    json[key] = dict[key]

            #判断validata
            validate = yamlDict['validate']
            if validate != None:
                for key in validate.keys():
                    validate[key] = dict[key]

            requestDataList.append(yamlDict)

        logger.info("组合请求数据为："+ str(requestDataList))
        return requestDataList

if __name__ == "__main__":
    result = RequestDataUtil('enJ')
    result.getRequestData()